<?php
	function chong_pha_hoai()
	{
	}
	 
   function thongbao($c)
   {
   		?>
			<script type="text/javascript">
				alert("<?php echo $c; ?>");
			</script>
		<?php
   }
   
   function trangtruoc()
   {
   		?>
			<script type="text/javascript">
				window.history.back();
			</script>
		<?php
		exit();
   }
   
   function chuyentrang($link)
   {
   		?>
			<script type="text/javascript">
				window.location="<?php echo $link; ?>";
			</script>
		<?php
		exit();
   }
   
   function xac_dinh_menu_con_123($id_cha)
   {
   		//$id_cha=$_GET['id'];
      $tv="select * from loaisp where(thuoc_id_loaisp='$id_cha' and isActive=1)";
	  $tv_1=mysql_query($tv);
      $tv_2=mysql_fetch_row($tv_1);
      if($tv_2==0)
      {
         return "no";
      }
      else
      {
         return "yes";
      }
   }
   
   function de_quy_menu_fff($id_cha)
   {
   		$tv="select * from loaisp where (thuoc_id_loaisp='$id_cha')";
		$tv_1=mysql_query($tv);
		while($tv_2=mysql_fetch_array($tv_1))
		{
			$link="?thamso=xuat&id=$tv_2[id_loaisp]";
			//echo"<li>";
			//echo"<ul>";
				echo"<a href='$link'>";
					echo $tv_2['ten_loaisp'];
				echo"</a>";
				//$xac_dinh_menu_con_123=xac_dinh_menu_con_123($tv_2['id_loaisp']);
				if(xac_dinh_menu_con_123($tv_2['id_loaisp'])=="yes")
				{
					//echo "<ul>";
					//echo"<li>";
						de_quy_menu_fff($tv_2['id_loaisp']);
					//echo"</li>";
					//echo "</ul>";
				}
			//echo"</ul>";
			//echo"</li>";
		}
	}
   
   //HÀM SHOW SẢN PHẨM TRONG MENU CON KHI CLICK VÀO MENU CHA
   function lay_chuoi_menu_con_uuu($id_cha,$c="")
   {
   		$tv="select * from loaisp where (thuoc_id_loaisp='$id_cha' and isActive=1)";
		$tv_1=mysql_query($tv);
		while($tv_2=mysql_fetch_array($tv_1))
		{
			$c=$c."_".$tv_2['id_loaisp'];
			$a_tv="select count(*) from loaisp where (thuoc_id_loaisp='$tv_2[id_loaisp]' and isActive=1)";
			$a_tv_1=mysql_query($a_tv);
			$a_tv_2=mysql_fetch_row($a_tv_1);
			if($a_tv_2[0]!=0)
			{
				$c=lay_chuoi_menu_con_uuu($tv_2['id_loaisp'],$c);
			}
		}
		return $c;
   }
   function lay_chuoi_menu_con_ppp($id_cha)
   {
   		$c=lay_chuoi_menu_con_uuu($id_cha);
		if($c=="")
		{
			return $id_cha;
		}
		else
		{
			return $id_cha.$c;
		}
   }
   function lay_mang_menu_con_ppp($id_cha)
   {
   		$c=lay_chuoi_menu_con_ppp($id_cha);
		$m=explode("_",$c);
		return $m;
		
   }
   function tinh_chuoi_union_ppp()
   {
   		$m=lay_mang_menu_con_ppp($_GET['id']);
		$tv="";
		for($i=0;$i<count($m);$i++)
		{
			$id=$m[$i];
			$r_tv="select count(*) from sanpham where (id_loaisp='$id' and isActive=1)";
			$r_tv_1=mysql_query($r_tv);
			$r_tv_2=mysql_fetch_row($r_tv_1);
			$so=$r_tv_2[0];
			$tv=$tv." ( select * from sanpham where (id_loaisp='$id' and isActive=1) limit 0,$so) union";
		}
		$tv=substr($tv,0,-6);
		return $tv;
   }
   //==========================================================================================================
   //TÍNH SỐ TRANG
   function tinh_so_trang($so_gioi_han)
   {
   		$m=lay_mang_menu_con_ppp($_GET['id']);
		$so=0;
		for($i=0;$i<count($m);$i++)
		{
			$id=$m[$i];
			$r_tv="select count(*) from sanpham where (id_loaisp='$id' and isActive=1)";
			$r_tv_1=mysql_query($r_tv);
			$r_tv_2=mysql_fetch_row($r_tv_1);
			$so=$so+$r_tv_2[0];
		}
		$st=ceil($so/$so_gioi_han);
		return $st;
   }
   //==========================================================================================================
   //SẢN PHẨM TRANG INDEX
   function xuat_ra_trang_index($id)
   {
		echo "<table width='100%'>";
		$sql="select * from loaisp where id_loaisp=$id";
		$sql_1=mysql_query($sql);
		$sql_2=mysql_fetch_array($sql_1);
		$link="?thamso=xuat&id=$sql_2[id_loaisp]";
		echo "<a href='$link'>";
			echo "<div class='title_contain'>$sql_2[ten_loaisp]</div>";
		echo "</a>";
		$ssp_td=3;
		$m=3;
		$tv="select * from sanpham where id_loaisp=$id ORDER BY time DESC";
		$tv_1=mysql_query($tv);
		$tv_2=mysql_fetch_array($tv_1);
			for($i=1;$i<=$ssp_td;$i++)
			{
				if($i!=1){$tv_2=mysql_fetch_array($tv_1);}
				if($i==1){echo "<tr>";}
				if($tv_2['ten_sanpham']!="")
				{
					$link_chi_tiet="?thamso=chi_tiet_san_pham&id=$tv_2[id_sanpham]";
					
					echo "<td align='center'>";
						echo "<div id='thongtin' align='center'>";
							echo "<font color='#FF0000'><b>".number_format($tv_2['dongia_sanpham'])."VNĐ </b></font>";
						echo "</div>";
						echo "<div id='hinh'>";
							$link_hinh="images/sanpham/".$tv_2['hinh'];
							echo "<a href='$link_chi_tiet'>";
								echo "<img border=0 src='$link_hinh' height='225' width='175'/>";
							echo "</a>";
							echo "<br>";
							echo "<a href='$link_chi_tiet'>";
								echo "<b>".$tv_2['ten_sanpham']."</b>";
							echo "</a>";
							echo "<br>";
							echo "<form action=''>";
								echo "Số lượng: ";
								echo "<input style='width:50px' value='1' name='solg_nnn'>";
								echo "<br>";
								echo "<div style='overflow:hidden;width:5px;height:3px'></div>";
								echo "<input type='hidden' name='id_sp_nnn' value='$tv_2[id_sanpham]'>";
								echo "<input type='hidden' name='them_vao_gio_nnn' value='them_vao_gio_nnn'>";
								echo "<input type='submit' value='Thêm vào giỏ'>";
							echo "</form>";
						echo "</div>";
					echo "</td>";
				}	
				if($i==$ssp_td){echo "</tr>";}
			}//for
	echo "</table>";
   }
   
   //==========================================================================================================
   //SẢN PHẨM TRANG INDEX
   function xuat_sp_moi_nhat()
   {
   		echo "<table width='100%'>";
			echo "<div class='title_contain'>SẢN PHẨM MỚI CẬP NHẬT</div>";
			$ssp_td=3;
			$m=3;
			$tv="select * from sanpham where isActive=1 ORDER BY time DESC";
			$tv_1=mysql_query($tv);
			$tv_2=mysql_fetch_array($tv_1);
			for($i=1;$i<=$ssp_td;$i++)
			{
				if($i!=1){$tv_2=mysql_fetch_array($tv_1);}
				if($i==1){echo "<tr>";}
				if($tv_2['ten_sanpham']!="")
				{
					$link_chi_tiet="?thamso=chi_tiet_san_pham&id=$tv_2[id_sanpham]";
					echo "<td align='center'>";
						echo "<div id='thongtin' align='right'>";
							echo "<img src='images/new.gif' /><font color='#FF0000'><b>".number_format($tv_2['dongia_sanpham'])."VNĐ </b></font>";
						echo "</div>";
						echo "<div id='hinh'>";
							$link_hinh="images/sanpham/".$tv_2['hinh'];
							echo "<a href='$link_chi_tiet'>";
								echo "<img border=0 src='$link_hinh' height='225' width='175'/>";
							echo "</a>";
							echo "<br>";
							echo "<a href='$link_chi_tiet'>";
								echo "<b>".$tv_2['ten_sanpham']."</b>";
							echo "</a>";
							echo "<br>";
							echo "<form action=''>";
								echo "Số lượng: ";
								echo "<input style='width:50px' value='1' name='solg_nnn'>";
								echo "<br>";
								echo "<div style='overflow:hidden;width:5px;height:3px'></div>";
								echo "<input type='hidden' name='id_sp_nnn' value='$tv_2[id_sanpham]'>";
								echo "<input type='hidden' name='them_vao_gio_nnn' value='them_vao_gio_nnn'>";
								echo "<input type='submit' value='Thêm vào giỏ'>";
							echo "</form>";
						echo "</div>";	
					echo "</td>";
				}	
				if($i==$ssp_td){echo "</tr>";}
			}//for
		echo "</table>";
   }
   //==========================================================================================================
   //ĐẾM LƯỢC TRUY CẬP
   function online()
	{
    $rip = $_SERVER['REMOTE_ADDR'];
    $sd = time();
    $count = 1;
    $maxu = 1;

    $file1 = "counter/online.log";
    $lines = file($file1);
    $line2 = "";

    foreach ($lines as $line_num => $line)
    {
        if($line_num == 0)
        {
            $maxu = $line;
        }
        else
        {
            $fp = strpos($line,'****');
            $nam = substr($line,0,$fp);
            $sp = strpos($line,'++++');
            $val = substr($line,$fp+4,$sp-($fp+4));
            $diff = $sd-$val;

            if($diff < 300 && $nam != $rip)
            {
                $count = $count+1;
                $line2 = $line2.$line;
            }
        }
    }

    $my = $rip."****".$sd."++++\n";
    if($count > $maxu)
    $maxu = $count;

    $open1 = fopen($file1, "w");
    fwrite($open1,"$maxu\n");
    fwrite($open1,"$line2");
    fwrite($open1,"$my");
    fclose($open1);
    $count=$count;
    $maxu=$maxu+200;
    
    return $count;
}

//
    $ip = $_SERVER['REMOTE_ADDR'];
    
    $file_ip = fopen('chuc_nang/counter/ip.txt', 'rb');
    while (!feof($file_ip)) $line[]=fgets($file_ip,1024);
    for ($i=0; $i<(count($line)); $i++) {
        list($ip_x) = split("\n",$line[$i]);
        if ($ip == $ip_x) {$found = 1;}
    }
    fclose($file_ip);
    
    if (!($found==1)) {
        $file_ip2 = fopen('chuc_nang/counter/ip.txt', 'ab');
        $line = "$ip\n";
        fwrite($file_ip2, $line, strlen($line));
        $file_count = fopen('chuc_nang/counter/count.txt', 'rb');
        $data = '';
        while (!feof($file_count)) $data .= fread($file_count, 4096);
        fclose($file_count);
        list($today, $yesterday, $total, $date, $days) = split("%", $data);
        if ($date == date("Y m d")) $today++;
            else {
                $yesterday = $today;
                $today = 1;
                $days++;
                $date = date("Y m d");
            }
        $total++;
        $line = "$today%$yesterday%$total%$date%$days";
        
        $file_count2 = fopen('chuc_nang/counter/count.txt', 'wb');
        fwrite($file_count2, $line, strlen($line));
        fclose($file_count2);
        fclose($file_ip2);
      }
      
      
    function today()
    {
        $file_count = fopen('chuc_nang/counter/count.txt', 'rb');
        $data = '';
        while (!feof($file_count)) $data .= fread($file_count, 4096);
        fclose($file_count);
        list($today, $yesterday, $total, $date, $days) = split("%", $data);
        return $today;
    }
    function yesterday()
    {
        $file_count = fopen('chuc_nang/counter/count.txt', 'rb');
        $data = '';
        while (!feof($file_count)) $data .= fread($file_count, 4096);
        fclose($file_count);
        list($today, $yesterday, $total, $date, $days) = split("%", $data);
        return $yesterday;
    }
    function total()
    {
        $file_count = fopen('chuc_nang/counter/count.txt', 'rb');
        $data = '';
        while (!feof($file_count)) $data .= fread($file_count, 4096);
        fclose($file_count);
        list($today, $yesterday, $total, $date, $days) = split("%", $data);
        echo $total;
    }
    function avg()
    {
        $file_count = fopen('chuc_nang/counter/count.txt', 'rb');
        $data = '';
        while (!feof($file_count)) $data .= fread($file_count, 4096);
        fclose($file_count);
        list($today, $yesterday, $total, $date, $days) = split("%", $data);
        echo ceil($total/$days);
    }
?>